package com.yuyang.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.yuyang.common.to.mq.SeckillOrderTo;
import com.yuyang.gulimall.order.entity.OrderEntity;
import com.yuyang.gulimall.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author yu yang
 * @date 2023/2/13 -22:18
 */
@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {
    @Autowired
    OrderService orderService;
    @RabbitHandler
    public void listenerOrder(Message message, SeckillOrderTo order, Channel channel) throws IOException {
        System.out.println("创建秒杀订单==》"+order.getOrderSn());
        try {
            orderService.createSeckillOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
